package seg.appx;

import geom.Line;
import geom.Point;
import geom.Rect;
import geom.gVector;


public class LineSegment extends Primitive {

	public static final int NUM_OF_DIFF = 100;

	public static final double LINE_THRESHOLD = 10;

	private Line line;

	public LineSegment() {
	}

	public double differenceAt(int i) {
		return line.ptLineDist(getPoints()[i].toPoint());

	}

	public void calcApproximation() {
		Rect bound = Rect.ofBound(getPoints());
		line = bound.getMajorAxis();
	}

	public double calcDifference() {
//		double diff = 0;
//		for (int i = 1; i < NUM_OF_DIFF; i++) {
//			double prop = (double)i / (double) NUM_OF_DIFF;
//			diff += differenceAt(prop);
//		}
//		System.err.println(diff + ", " + NUM_OF_DIFF);
//		return diff / NUM_OF_DIFF;

		double diff = 0;
		for (int ii = 0; ii < getPoints().length; ii++) {
			diff += line.ptLineDist(getPoints()[ii].toPoint());
		}
		return diff / (double) getPoints().length;
	}

	public boolean isApproximate() {
		return ( calcDifference() ) < LINE_THRESHOLD;
	}

	public gVector normalVectorAt(double prop) {
		return line.dir().rotate(.5 * Math.PI);
	}

	public Point pointAt(double prop) {
		if (prop < 0 || prop > 1)
			return null;
		return line.linearInterpolate(prop);
	}


}
